package com.zhengshang.api.controller;

import com.zhengshang.api.domain.AppOilcard;
import com.zhengshang.api.service.IAppOilcardService;
import com.zhengshang.common.annotation.Log;
import com.zhengshang.common.core.controller.BaseController;
import com.zhengshang.common.core.domain.AjaxResult;
import com.zhengshang.common.core.domain.entity.SysDictData;
import com.zhengshang.common.core.domain.model.LoginAppUser;
import com.zhengshang.common.utils.StringUtils;
import com.zhengshang.security.service.TokenService;
import com.zhengshang.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 用户加油卡Controller
 *
 * @author zhengshang
 * @date 2022-04-10
 */
@RestController
@RequestMapping("/oilcard")
public class OilcardController extends BaseController {

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private IAppOilcardService appOilcardService;

    @Autowired
    private TokenService tokenService;

    /**
     * 获取油卡类型类列表
     */
    @GetMapping(value = "/oilcardType/list")
    @ResponseBody
    public AjaxResult getOilcardTypeList() {
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType("oilcard_type");
        List<SysDictData> list = dictDataService.selectDictDataList(sysDictData);
        return AjaxResult.success(list);
    }

    /**
     * 获取油卡类型类列表
     */
    @GetMapping(value = "/getOilcardList")
    @ResponseBody
    public AjaxResult getOilcardList(HttpServletRequest request) {
        LoginAppUser loginAppUser = tokenService.getLoginAppUser(request);
        Long userId = loginAppUser.getAppUser().getUserId();
        AppOilcard appOilcard = new AppOilcard();
        appOilcard.setUserId(userId);
        appOilcard.setDelFlag("0");
        List<Map> appOilcardList = appOilcardService.queryAppOilcardList(appOilcard);
        return AjaxResult.success(appOilcardList);
    }

    /**
     * 新增用户油卡
     */
    @Log(title = "新增用户加油卡")
    @PostMapping(value = "/save")
    public AjaxResult save(HttpServletRequest request, AppOilcard appOilcard) {
        String oilcardTypeId = appOilcard.getOilcardTypeId();
        if (StringUtils.isEmpty(oilcardTypeId)) {
            return AjaxResult.error("油卡类型必传");
        }
        String userName = appOilcard.getUserName();
        if (StringUtils.isEmpty(userName)) {
            return AjaxResult.error("姓名必传");
        }
        String identityNo = appOilcard.getIdentityNo();
        if (StringUtils.isEmpty(identityNo)) {
            return AjaxResult.error("身份证号必传");
        }
        String oilcardNo = appOilcard.getOilcardNo();
        if (StringUtils.isEmpty(oilcardNo)) {
            return AjaxResult.error("油卡卡号必传");
        }
        LoginAppUser loginAppUser = tokenService.getLoginAppUser(request);
        Long userId = loginAppUser.getAppUser().getUserId();
        Integer isDefault = appOilcard.getIsDefault();
        if (StringUtils.isNull(isDefault)) {
            isDefault = 0;
        }
        if (isDefault == 1) {
            appOilcardService.changeDefault(userId, 0);
        }
        appOilcard.setUserId(userId);
        return toAjax(appOilcardService.insertAppOilcard(appOilcard));
    }

    /**
     * 修改用户加油卡
     */
    @Log(title = "修改用户加油卡")
    @PostMapping(value = "/update")
    public AjaxResult update(HttpServletRequest request, AppOilcard appOilcard) {
        LoginAppUser loginAppUser = tokenService.getLoginAppUser(request);
        Long userId = loginAppUser.getAppUser().getUserId();
        Integer isDefault = appOilcard.getIsDefault();
        if (StringUtils.isNull(isDefault)) {
            isDefault = 0;
        }
        if (isDefault == 1) {
            appOilcardService.changeDefault(userId, 0);
        }
        return toAjax(appOilcardService.updateAppOilcard(appOilcard));
    }

    /**
     * 删除用户加油卡
     */
    @Log(title = "逻辑删除用户加油卡")
    @PostMapping("/logicDelete")
    public AjaxResult delete(Long oilcardId) {
        AppOilcard appOilcard = appOilcardService.selectAppOilcardById(oilcardId);
        appOilcard.setDelFlag("2");
        return toAjax(appOilcardService.updateAppOilcard(appOilcard));
    }
}
